define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
    	index: function () {
    		$("#TogglePreviewTableBtn").on("click",function(){
		        if ($(this).hasClass("getoutline-close")){
		            $(this).removeClass("getoutline-close");
		            $(this).addClass("getoutline-open");
		            $(".getoutline-fade").removeClass("toggleable-hidden");
		        }else{
		            $(this).removeClass("getoutline-open");
		            $(this).addClass("getoutline-close");
		            $(".getoutline-fade").addClass("toggleable-hidden");
		        } 
		    });
			$(document).on("click",".ajax-btn",function(){
				var memo=$(this).attr("id");
				ajax_page(memo);
			})
			$(document).on("click",".overtime-getOutline",function(){
				getOutline();
			})
			$(document).on("click",".overtime-getTimeTrendRpt",function(){
				var memo=$(this).attr("data-memo");
				getTimeTrendRpt(memo);
			})
			$(document).on("click",".overtime-getCommonTrackRpt",function(){
				var memo=$(this).attr("data-memo");
				getCommonTrackRpt(memo)
			})
			var ajax_page=function (memo){
			    $(".btn-group .btn").addClass("btn-default");
			    $(".btn-group .btn").removeClass("btn-primary");
			    $("#"+memo).addClass("btn-primary");
			    $("#"+memo).removeClass("btn-default");
			    getTimeTrendRpt(memo);
			    getCommonTrackRpt(memo);
			};
			var getOutline=function(){
				var overtime='<tr><td colspan="7" style="text-align:center;">请求超时，<a href="javascript:;" class="overtime-getOutline">点击重试</a></td></tr>';
				$.ajax({
			        url:url_getoutline,
			        type:'post',
			        timeout: '10000',
			        beforeSend:function(){
			            $('.getoutline-tbody').html('<tr><td colspan="7" style="text-align:center;"><i class="fa fa-spin fa-spinner" aria-hidden="true"></i>&nbsp;&nbsp;数据获取中...</td></tr>');
			        },
			        success:function(res){
			            $('.getoutline-tbody').html(res.html);
			        },
			        error:function(XMLHttpRequest, textStatus, errorThrown){
			            $('.getoutline-tbody').html(overtime); 
			        },
			        complete: function (XMLHttpRequest, textStatus) {
			            if(textStatus == 'timeout'){
			                $('.getoutline-tbody').html(overtime);
			            }
			        },
			    });
			};
			var getTimeTrendRpt=function(memo){
				$("#echarts-line-chart").css("width",$("#echarts-line-chart").width());
				var overtime='请求超时，<a href="javascript:;" class="overtime-getTimeTrendRpt" data-memo="'+memo+'">点击重试</a>';
			    $.ajax({
			        url:url_gettimetrendrpt,
			        type:'post',
			        data:'memo='+memo,
			        dataType: "json",
			        timeout: '10000',
			        success:function(res){
			            if(res.status==1){
			                var echarts_getTimeTrendRpt = echarts.init(document.getElementById("echarts-line-chart")),
			                option_getTimeTrendRpt = {
			                    tooltip: {
			                        trigger: "axis"
			                    },
			                    grid: {
			                        left: '3%',
			                        right: '5%',
			                        bottom: '3%',
			                        containLabel: true
			                    },
			                    legend: {
			                        data: ['浏览量（PV）','访客数（UV）']
			                    },
			                    calculable: !0,
			                    xAxis: [{
			                        type: "category",
			                        boundaryGap: !1,
			                        data: res.data.date,
			                        axisTick: {
			                            alignWithLabel: true
			                        }
			                    }],
			                    yAxis: {
			                        type: "value",
			                    },
			                    series: [
			                        {
			                            name: "浏览量（PV）",
			                            type: "line",
			                            data: res.data.pv,
			                            areaStyle:{
			                                normal:{
			                                    color:['#e5f1fd']
			                                }
			                            }
			                        },
			                        {
			                            name: "访客数（UV）",
			                            type: "line",
			                            data: res.data.uv,
			                            areaStyle:{
			                                normal:{
			                                    color:['#e5f1fd']
			                                }
			                            }
			                        }
			                    ],
			                    color:['#4fa8f9','#6ec71e']
			                };
			                echarts_getTimeTrendRpt.setOption(option_getTimeTrendRpt);
			                $(window).resize(function(){
			                   echarts_getTimeTrendRpt.resize({width:$("#echarts-line-chart").closest(".panel-body").width()})
			                });
			            }else{
			                $("#echarts-line-chart span").html('暂无数据');
			            }
			        },
			        error:function(XMLHttpRequest, textStatus, errorThrown){
			            $("#echarts-line-chart span").html(overtime);
			        },
			        complete: function (XMLHttpRequest, textStatus) {
			            if(textStatus == 'timeout'){
			               $("#echarts-line-chart span").html(overtime);
			            }
			        },
			    });
			};
			var getCommonTrackRpt=function(memo){
				var overtime='<tr><td colspan="7" style="text-align:center;">请求超时，<a href="javascript:;" class="overtime-getCommonTrackRpt" data-memo="'+memo+'">点击重试</a></td></tr>';
				$.ajax({
			        url:url_getcommontrackrpt,
			        type:'post',
			        data:'memo='+memo,
			        dataType: "json",
			        timeout: '10000',
			        beforeSend:function(){
			            $('.visit-overview-tbody').html('<tr><td colspan="7" class="no-borders text-center"><span style="line-height: 300px;"><i class="fa fa-spin fa-spinner" aria-hidden="true"></i>&nbsp;&nbsp;数据获取中...<span></td></tr>');
			        },
			        success:function(res){
			            if(res.status){
			                $("#sourceSite").html(res.data.sourceSite);
			                $("#word").html(res.data.word);
			                $("#landingPage").html(res.data.landingPage);
			                $("#visitPage").html(res.data.visitPage);
			                $(".new_ratio span").html(res.data.visitType.newVisitor.ratio);
			                $(".old_ratio span").html(res.data.visitType.oldVisitor.ratio);
			                $(".new_pv_count").html(res.data.visitType.newVisitor.pv_count);
			                $(".old_pv_count").html(res.data.visitType.oldVisitor.pv_count);
			                $(".new_visitor_count").html(res.data.visitType.newVisitor.visitor_count);
			                $(".old_visitor_count").html(res.data.visitType.oldVisitor.visitor_count);
			                $(".new_bounce_ratio").html(res.data.visitType.newVisitor.bounce_ratio+"%");
			                $(".old_bounce_ratio").html(res.data.visitType.oldVisitor.bounce_ratio+"%");
			                $(".new_avg_visit_time").html(res.data.visitType.newVisitor.avg_visit_time);
			                $(".old_avg_visit_time").html(res.data.visitType.oldVisitor.avg_visit_time);
			                $(".new_avg_visit_pages").html(res.data.visitType.newVisitor.avg_visit_pages);
			                $(".old_avg_visit_pages").html(res.data.visitType.oldVisitor.avg_visit_pages);
			            }else{
			                var msg='<tr><td colspan="3" class="text-center">暂无数据</td></tr>';
			                $("#sourceSite").html(msg);
			                $("#word").html(msg);
			                $("#landingPage").html(msg);
			                $("#visitPage").html(msg);
			            }
			        },
			        error:function(XMLHttpRequest, textStatus, errorThrown){
			            $('.visit-overview-tbody').html(overtime);
			        },
			        complete: function (XMLHttpRequest, textStatus) {
			            if(textStatus == 'timeout'){
			                $('.visit-overview-tbody').html(overtime);
			            }
			        },
			    });
			};
			if(bdtj_show==1){
				getOutline();
				ajax_page('tody');
			}else{
				layer.alert("尚未配置，请前往插件管理-百度网站统计-配置");
			}
    	}
    };
    return Controller;
});